package cn.xiaoliu.org.controller;

import cn.xiaoliu.basic.utils.AjaxResult;
import cn.xiaoliu.basic.utils.PageList;
import cn.xiaoliu.org.domain.Department;
import cn.xiaoliu.org.domain.Dictionary;
import cn.xiaoliu.org.domain.DictionaryType;
import cn.xiaoliu.org.query.DictionaryQuery;
import cn.xiaoliu.org.service.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dic")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;

    //分页加高级查询  post
    @PostMapping("/list")
    public PageList<Dictionary> queryPage(@RequestBody DictionaryQuery query){
        return dictionaryService.queryPage(query);
    }

    //删除  delete
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            dictionaryService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }
    //修改新增  put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionary dictionary){
        try {
            if (dictionary.getId()==null){
                dictionaryService.add(dictionary);
            }else {
                dictionaryService.update(dictionary);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }

    //批量删除
    @PatchMapping
    public AjaxResult pachRemove(@RequestBody List<Long> ids){
        try {
            dictionaryService.pachRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }
    @GetMapping("/{id}")
    public List<Dictionary> queryById(@PathVariable("id") Long id){
        return dictionaryService.queryByTypeId(id);
    }
}
